package com.ticket.sass.admin.handler



import com.ticket.sass.admin.model.ResultModel
import com.ticket.sass.common.exception.enums.ErrorCode
import com.ticket.sass.common.exception.exception.*
import com.ticket.sass.common.exception.exception.InternalException
import com.ticket.sass.common.exception.exception.UnauthorizedException
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.boot.autoconfigure.web.servlet.error.AbstractErrorController
import org.springframework.boot.autoconfigure.web.servlet.error.ErrorViewResolver
import org.springframework.boot.web.error.ErrorAttributeOptions
import org.springframework.boot.web.servlet.error.ErrorAttributes
import org.springframework.http.converter.HttpMessageNotReadableException
import org.springframework.web.bind.annotation.ControllerAdvice
import org.springframework.web.bind.annotation.ExceptionHandler
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.ResponseBody
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse


@ControllerAdvice
@RequestMapping("/error")
class ErrorController(
    val errorAttributes: ErrorAttributes?,
    errorViewResolvers: MutableList<ErrorViewResolver>?
) : AbstractErrorController(errorAttributes, errorViewResolvers) {
    private val logger: Logger = LoggerFactory.getLogger(ErrorController::class.java)

    private val errorAttributeOptions = ErrorAttributeOptions.of(
        ErrorAttributeOptions.Include.MESSAGE,
        ErrorAttributeOptions.Include.STACK_TRACE,
        ErrorAttributeOptions.Include.EXCEPTION
    )

    fun getErrorPath(): String? {
        return null
    }

    @ExceptionHandler(
        InternalException::class,
        ConcurrentException::class,
        BadRequestException::class,
        ConfigException::class,
        UnauthorizedException :: class
    )
    @ResponseBody
    fun internalException(request: HttpServletRequest?, response: HttpServletResponse?, exception: BaseException): ResultModel<Any?> {
        val body: Map<String, Any> = getErrorAttributes(request, errorAttributeOptions)

        logger.error(body["trace"].toString())

        response?.status = 200

        return ResultModel(
            exception.errorCode.value,
            exception.errorMessage ?: exception.errorCode.reasonPhrase
        )
    }

    @RequestMapping
    @ResponseBody
    @ExceptionHandler(HttpMessageNotReadableException::class)
    fun error(request: HttpServletRequest?, response: HttpServletResponse?): ResultModel<Any?> {
        val body: Map<String, Any> = getErrorAttributes(request, errorAttributeOptions)

        logger.error(body["trace"].toString())


        var message = ErrorCode.HTTP_TYPE_ERROR.reasonPhrase
        if (response?.status == 404) {
            message = "接口不存在"
        }

        response?.status = 200

        return ResultModel(
            ErrorCode.CLIENT_ERROR.value,
            message
        )
    }
}